## Written 2012 by Scott Hardin for the OpenXPKI project
## Copyright (C) 2012 by the OpenXPKI project
##
## Prereq: cpanspec.sourceforge.net

TOPDIR=../../../..

# Location of *.spec, *.spec2, and *.specdep
SPECDIR=spec

.SUFFIXES:

.PHONY: cpan

include ../Makefile.inc

# BUILDING CPAN DEPENDENCY MODULES
#
# This ain't as easy as you'd think it should be. I'll spare you the details.
#
# The following variables are set statically for each distro in an include
# file:
#
#	CPANMODS Contains the names of all of the CPAN modules that this
#	project depends on and must be packaged (i.e. not part of the OS distro).
#	To make these values "Makefile" compatible, the '::' is replaced with '__'.
#	The name also corresponds to the package names built, where the '__' is 
#	replaced with a '-'.
#
#	CPAN.$(MOD) Is a namespace for exceptions to the standard build process.
#	The following suffixes are used:
#
#		.searchname	- The module name passed to cpanspec when searching CPAN
#		
#		.specpatch - Name of patch file used to patch the .spec2 file
#
#   CPANNOBUILD Contains a list of all CPAN modules that should _not_ be built
#   because they are either a core module or there is already a package in
#   the distro.
#
# The following variables are automatically generated by make:
#
#	CPANFETCH Contains the CPAN module names as they are to be fetched
#	by cpanspec. In this list, the names may contain colons and are
#	therefore not to be used as a target in the makefile.
#
#	CPANSPECS Contains a list of the spec files for CPAN Modules to
#	be built. It is created by genmodlist.pl.
#
#	CPANSPECDEPS Contains a list of the .spec dependency files that are
#	generated by the specdep.sh script. The list is generated from CPANSPECS.
#
#	CPANRPMS Is populated with the RPM filenames by the specdep.sh script.
#
# Deprecated???
#
#	CPANBUILD Contains a list of all CPAN Modules that are to be built for
#	this distribution and distribution version. It is set in the files
#	*-cpan-build.inc, which is included by this Makefile.
#
#	Important Note: In the above variables, the double-colon separator 
#	is replaced with two underscores.
#
# The specdeps files are generated based on the version numbers in the
# spec files. These files are then 'included' by make and contain
# the dependency chain from the RPM and Tarball files to the spec files.
#
# NOTES:
#
# Some SPEC files created by cpanspec are not correct. To accomodate this,
# let the .spec2 file be created, modify a copy of it, correcting any errors,
# and then run the following:
#
# 	diff -u MODULENAME.spec2 MODULENAME.spec2.new > MODULENAME.spec2.patch
#
# Add this file to the OS-specific definitions for the corresponding package
# and this will get picked up by the rule that creates the .spec2 file

CPANRPMS:=

# Target for CPAN external modules is listed in an include file
DISTCPANINC := $(shell $(TOPDIR)/tools/distver)-cpan-build.inc
DISTCPANCFG := $(shell $(TOPDIR)/tools/distver)-cpan-build.cfg
# If a .local version of the config file exists, use it instead.
# Note: the local file must import the config from the upstream
# version.
DISTCPANCFGLOCAL := $(wildcard $(DISTCPANCFG).local)
ifdef DISTCPANCFGLOCAL
	DISTCPANCFG := $(DISTCPANCFGLOCAL)
endif

$(DISTCPANINC): $(DISTCPANCFG) cpandeps.pl
	@echo "DISTCPANCFG=$(DISTCPANCFG)"
	./cpandeps.pl --config $< --out $@

include $(DISTCPANINC)
-include $(DISTCPANINC).local

CPANSPECDEPS:=$(addsuffix dep,$(CPANSPECS))

debug:
	@echo "CPANMODS=$(CPANMODS)"
	@echo
	@echo "CPANFETCH=$(CPANFETCH)"
	@echo
	@echo "CPANSPECS=$(CPANSPECS)"
	@echo
	@echo "CPANSPECDEPS=$(CPANSPECDEPS)"
	@echo
	@echo "CPANRPMS=$(CPANRPMS)"

$(CPANSPECS):
	mkdir -p $(SPECDIR)
	(cd $(SPECDIR) && cpanspec --installbase=/usr/local --force --license --old --follow $(CPANFETCH))

fetchcpan: $(CPANSPECS)

export CPANNOBUILD

%.specdep: %.spec cpandeps.pl $(DISTCPANCFG)
	./cpandeps.pl --config $(DISTCPANCFG) --spec $< --out $@

ifdef CPANSPECDEPS
-include $(CPANSPECDEPS)
endif

cpan: $(CPANRPMS)

PERL5LIB:="$(PERLINC):$(PERL5LIB)"

export PERL5LIB

clean:
	rm -f spec/*.spec2 spec/*.specdep
	@echo "NOTE: You must manually clean /usr/src/packages/BUILD"

# WARNING: This one is a bit destructive in the RPM directory!!!
realclean: clean
	rm -rf $(RPMBASE)/RPMS/*/perl-*.rpm
	for i in $(RPMBASE)/BUILD/*; do \
		test -f $i/blib/built && echo "# rm -rf $i"; \
	done
